/* @(#)GSManufactureCategoryDAO.java    
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-13    新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.hitrust.gs.bean.GSAlignmentBean;
import com.hitrust.gs.bean.GSCountAndSumBean;
import com.hitrust.gs.bean.GSInHeadBean;
import com.hitrust.gs.bean.GSInSubBean;
import com.hitrust.gs.bean.GSOutHeadBean;
import com.hitrust.gs.bean.GSOutSubBean;
import com.hitrust.gs.bean.GSWhStaffBean;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 
 */
public class GSBusAlignmentDAO extends GSBaseDAO {
    DateUtil tDate = new DateUtil();
    String sdf = "yyyy-MM-dd";
    /**
     * 在产品种类表中新增一笔记录
     * 
     * @param aGSManufactureBean
     * 
     * return boolean
     * 
     * @throws CUPSException
     */
    public boolean add(GSAlignmentBean aGSAlignmentBean) throws GSException {
        String tSql = "";
        Object[] tObject;
            tSql="INSERT into T_ACTION_ALIGNMENT (ID,KEYID,ACTIONNAME,FILEDTIMES,REMARK,CONTENT) VALUES  ((select SYS_GUID() from dual),?,?,?,?,?)"; 
            tObject = new Object[5] ;
            tObject[0]=aGSAlignmentBean.getKEYID();
            tObject[1]=aGSAlignmentBean.getACTIONNAME();
            tObject[2]=aGSAlignmentBean.getFILEDTIMES();
            tObject[3]=aGSAlignmentBean.getREMARK();
            tObject[4]=aGSAlignmentBean.getCONTENT();
  
//          for(int i =0;i<tObject.length;i++){
//              if(tObject[i]==null){
//                  tObject[i]="";
//              }
//          }
          
//          tObject[0]="11";
//          tObject[1]="test";
//          tObject[2]="2";
//          tObject[3]="0";
//          tObject[4]="test";
//          tObject[5]=aGSAlignmentBean.getCONTENT();
//          tObject[6]="2009-05-06";
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            ex.printStackTrace();
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.toString());
        }

        return tResult;
    }
    
    public ArrayList searchTask() throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_ACTION_ALIGNMENT";
        try{
            tList = this.executeQuery(new GSAlignmentBean(),tSql,null);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public boolean delTask(String Id) throws GSException{
        String tSql = "delete from T_ACTION_ALIGNMENT where ID=?";
        Object[] tObject = { Id };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    public boolean delExistTask(String actionName,String remark) throws GSException{
        String tSql = "delete from T_ACTION_ALIGNMENT where ACTIONNAME= ? and REMARK = ?";
        Object[] tObject = { actionName,remark };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    public boolean modify(GSAlignmentBean aGSAlignmentBean,String responseCode) throws GSException {

        String tSql = "UPDATE T_ACTION_ALIGNMENT SET FILEDTIMES = ?,TYPE=?," +
                "LAST_UPDATE_TIME=? WHERE ID = ?";
     
        Connection conn = null;
        boolean tResult = false;
        try {
            conn = this.getConnetion();
            PreparedStatement pstmt = conn.prepareStatement(tSql);
            int times = Integer.parseInt(aGSAlignmentBean.getFILEDTIMES())+1;
            pstmt.setInt(1, times);
            String type = responseCode.toString();
            pstmt.setString(2,type);
            String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
            pstmt.setString(3,date);
            pstmt.setString(4,aGSAlignmentBean.getID());
            pstmt.executeUpdate();
            pstmt.close();
            tResult = true;
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        } finally{
            try{
                conn.close();
            }catch(Exception e){
                
            }
        }

        return tResult;
    }
    
    public ArrayList searchTaskIsExist(String actionName,String Remark) throws GSException {

        ArrayList tList = new ArrayList();
        String tSql = "select * from T_ACTION_ALIGNMENT where ACTIONNAME=? and REMARK=?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[2] ;
            tObject[0]=actionName;
            tObject[1]=Remark;
            tList = this.executeQuery(new GSAlignmentBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchInHead(String inBillCode) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_IN_HEAD where IN_BILL_CODE=?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[1] ;
            tObject[0]=inBillCode;
            tList = this.executeQuery(new GSInHeadBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchInHead(String startDate,String endDate) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_IN_HEAD where create_time>=? and create_time< ?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[2] ;
            tObject[0]=startDate;
            tObject[1]=endDate;
            tList = this.executeQuery(new GSInHeadBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchInSub(String inBillCode) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_IN_SUB where IN_BILL_CODE=?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[1] ;
            tObject[0]=inBillCode;
            tList = this.executeQuery(new GSInSubBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchInSub(String startDate,String endDate) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_IN_SUB where create_time>=? and create_time< ?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[2] ;
            tObject[0]=startDate;
            tObject[1]=endDate;
            tList = this.executeQuery(new GSInSubBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchOutHead(String outBillCode) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_OUT_HEAD where OUT_BILL_CODE=?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[1] ;
            tObject[0]=outBillCode;
            tList = this.executeQuery(new GSOutHeadBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    
    public ArrayList searchOutHead(String startDate,String endDate) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_OUT_HEAD where create_time>=? and create_time< ?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[2] ;
            tObject[0]=startDate;
            tObject[1]=endDate;
            tList = this.executeQuery(new GSOutHeadBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchOutSub(String outBillCode) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_OUT_SUB where OUT_BILL_CODE=?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[1] ;
            tObject[0]=outBillCode;
            tList = this.executeQuery(new GSOutSubBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchOutSub(String startDate,String endDate) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select * from T_OUT_SUB where create_time>=? and create_time< ?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[2] ;
            tObject[0]=startDate;
            tObject[1]=endDate;
            tList = this.executeQuery(new GSOutSubBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchProductCount(String productCode,String whCode) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "select product_code,sum(product_count) count,sum(product_money) sum from t_product_storage where product_id = ? and warehouse_code = ? group by product_code ";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[2] ;
            tObject[0]=productCode;
            tObject[1]=whCode;
            tList = this.executeQuery(new GSCountAndSumBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public ArrayList searchWarehouseStaff(String pid,String whcode) throws GSException{
        ArrayList tList = new ArrayList();
        String tSql = "SELECT code, NAME FROM t_warehouse_staff a, t_warehouse_product b WHERE a.ID = b.keeper_staff_id AND b.product_id = ? AND b.warehouse_code = ?";
        //Connection conn = null;
        try{
            //conn = this.getConnetion();
            //PreparedStatement pstmt = conn.prepareStatement(tSql);
            Object[] tObject = new Object[2] ;
            tObject[0]=pid;
            tObject[1]=whcode;
            tList = this.executeQuery(new GSWhStaffBean(),tSql,tObject);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    

}
